package com.edu.base.rest;

import com.edu.base.common.utils.DateUtils;
import com.edu.base.common.utils.IdWorker;
import com.edu.base.ums.member.domain.ClsMember;
import com.edu.base.ums.member.mapper.ClsMemberMapper;
import com.edu.base.ums.member.service.IClsMemberService;
import com.edu.base.ums.memberAccountRecord.domain.ClsMemberAccountRecord;
import com.edu.base.ums.memberAccountRecord.domain.PointVo;
import com.edu.base.ums.memberAccountRecord.mapper.ClsMemberAccountRecordMapper;
import com.edu.base.ums.memberShareRecord.domain.ClsMemberShareRecord;
import com.edu.base.ums.memberShareRecord.service.IClsMemberShareRecordService;
import com.google.common.collect.Lists;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.List;

/**
 * 积分 
 * @author admin
 */
@Api(tags = "A、我的邀请码接口")
@RestController
@RequestMapping("/api/invicateCode")
public class AppMembernIviteCodeController extends BaseController {
 
	 @Autowired
	 private IClsMemberService  clsMemberService;
	 
	 @Autowired
	 private ClsMemberMapper  clsMemberMapper;
	 
	 @Autowired
	 private ClsMemberAccountRecordMapper clsMemberAccountRecordMapper;

	@Autowired
	 private IClsMemberShareRecordService clsMemberShareRecordService;

 
    @GetMapping(value ="/getMemberInvicateCode")
   	@ApiOperation("获取会员邀请码")
    @ApiImplicitParams({
 	    @ApiImplicitParam(name = "memberId", value = "会员ID"),
    })
    public AjaxResult getInvicateCode(String memberId)
    {
    	ClsMember cls =  clsMemberService.selectClsMemberById(memberId);
    	if(cls == null) {
    		return AjaxResult.error("会员信息不存在");
    	}
    	String inviteCode = cls.getInviteCode();
    	if(StringUtils.isBlank(inviteCode)) {
    		inviteCode = StringUtils.getRandomCode(6);//ShareCodeUtil.generateCode();
    		cls.setInviteCode(inviteCode);
    		clsMemberMapper.updateClsMember(cls);
    	} 
        return AjaxResult.success(inviteCode);
    }

    @PostMapping("AddRecord")
	@ApiOperation("添加分享记录")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "memberId", value = "会员ID",required = true),
			@ApiImplicitParam(name = "inviteCode", value = "邀请码",required = true),
			@ApiImplicitParam(name = "shareType", value = "分享类型，1 微信，2 朋友圈",required = true)
	})
	public AjaxResult AddRecord(String memberId,String inviteCode, String shareType){
		ClsMemberShareRecord shareRecord = new ClsMemberShareRecord();
		shareRecord.setMemberId(memberId);
		shareRecord.setInviteCode(inviteCode);
		shareRecord.setInviteCode(inviteCode);
		shareRecord.setShareTime(new Date());
    	return AjaxResult.success(clsMemberShareRecordService.insertClsMemberShareRecord(shareRecord));
	}
    
    @GetMapping(value ="/getShareReginCode")
   	@ApiOperation("获取会员邀请码分享注册明细")  
    @ApiImplicitParams({
 	    @ApiImplicitParam(name = "memberId", value = "会员ID"),
    })
	@ApiIgnore
    public AjaxResult getShareReginCode(String memberId) {
    	
    	ClsMemberAccountRecord vo =new ClsMemberAccountRecord();  
    	vo.setChangeType(40001); //分享注册
    	vo.setMemberId(memberId);
    	List<ClsMemberAccountRecord>  list = clsMemberAccountRecordMapper.selectClsMemberAccountRecordList(vo);
    	
    	List<PointVo>   plist = Lists.newLinkedList();
    	for (ClsMemberAccountRecord car : list) {
    		PointVo  pointVo = new PointVo();
    		pointVo.setChangePoint(car.getChangePoint());
    		pointVo.setTransactionTime(DateUtils.formatDate(car.getCreateDate(), "yyyy.MM.dd MM:dd"));
    		// TODO 手机号进行脱敏操作
    		String mask = car.getOperatePhone().substring(0, 3)+ "****" + car.getOperatePhone().substring(car.getOperatePhone().length()-4,car.getOperatePhone().length());
    		pointVo.setPhone(mask);
    		plist.add(pointVo);
		}
        return AjaxResult.success(plist); 
    }
    
    
    @GetMapping(value ="/getConsumerRewardsCode")
   	@ApiOperation(value = "获取会员邀请码好友消费奖励明细",response= PointVo.class)  
    @ApiImplicitParams({
 	    @ApiImplicitParam(name = "memberId", value = "会员ID"),
    })
	@ApiIgnore
    public AjaxResult getConsumerRewardsCode(String memberId) {
    	
    	ClsMemberAccountRecord vo =new ClsMemberAccountRecord();  
    	vo.setChangeType(40002); // 好友消费奖励
    	vo.setMemberId(memberId);
    	List<ClsMemberAccountRecord>  list = clsMemberAccountRecordMapper.selectClsMemberAccountRecordList(vo);
    	
    	List<PointVo>   plist = Lists.newLinkedList();
    	for (ClsMemberAccountRecord car : list) {
    		PointVo  pointVo = new PointVo();
    		pointVo.setChangePoint(car.getChangePoint());
    		pointVo.setTransactionTime(DateUtils.formatDate(car.getCreateDate(), "yyyy.MM.dd MM:dd"));
    		String mask = car.getOperatePhone().substring(0, 3)+ "****" + car.getOperatePhone().substring(car.getOperatePhone().length()-4,car.getOperatePhone().length());
    		pointVo.setPhone(mask);
    		plist.add(pointVo); 
		}
        return AjaxResult.success(plist); 
    }
    
    
    /**
     * 新增账户流水记录 包含金额、积分
     */
  
    @PostMapping(value ="/addPoint")
    @ApiOperation("新增邀请码积分")  
    @ApiImplicitParams({
 	  @ApiImplicitParam(name = "memberId", value = "会员ID",required=true),
 	  @ApiImplicitParam(name = "inviteCode", value = "邀请码",required=true),
 	  @ApiImplicitParam(name = "operatePhone", value = "手机号",required=true),  
 	  @ApiImplicitParam(name = "changeType", value = "金额：30001 充值/ 30002 充会员/ 30003消费/30004 退款/ 30004 提现; \"\r\n" + 
 	  		"    		+ \"							积分：40001:推荐/ 40002推荐人消费/ 40003消费/ 40004消费返积分)"),  
    })
	@ApiIgnore
    public AjaxResult addPoint(String memberId,String  inviteCode,String operatePhone,String changeType)
	{
		ClsMember cls = clsMemberService.selectClsMemberById(memberId);
		if (cls != null && !cls.getInviteCode().equals(inviteCode)) {
			return AjaxResult.error("邀请码不正确");
		}
		if (org.apache.commons.lang3.StringUtils.isBlank(operatePhone)||operatePhone.length() != 11) {
			return AjaxResult.error("手机号不正确");
		}
		ClsMemberAccountRecord vo = new ClsMemberAccountRecord();

		vo.setCreateDate(DateUtils.getNow());
		vo.setOperatePhone(operatePhone);
		vo.setChangePoint(30l);
		vo.setId(IdWorker.idAutoIncrement());
		vo.setChangeType(Integer.valueOf(changeType));
		vo.setTransactionTime(new Date());
		return toAjax(clsMemberAccountRecordMapper.insertClsMemberAccountRecord(vo));
	}
    
    
    
 
}
